package com.zoe.onelink.business.i18n;

import com.zoe.onelink.business.dao.ComI18nDictLangInfoDao;
import com.zoe.onelink.business.dao.ParamDao;
import com.zoe.onelink.core.i18n.I18nMessageProvider;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.ApplicationContext;

import java.util.Locale;

/**
 * <p>标题: </p>
 * <p>描述: </p>
 * <p>版权: Copyright (c) 2022</p>
 * <p>公司: 智业软件股份有限公司</p>
 *
 * @version: 1.0
 * @author: ruansheng
 * @date: 2022-03-07
 */
@RequiredArgsConstructor
public class I18nBizCustomMessageProvider implements I18nMessageProvider, SmartInitializingSingleton {

    private final ApplicationContext applicationContext;

    private ComI18nDictLangInfoDao comI18nDictLangInfoDao;

    @Override
    @Cacheable(cacheNames = "I18N:", key = "#locale.language + ':' + #messageKey", unless = "#result == null", cacheManager = "caffeineCacheManager")
    public String getMessageTemplate(Locale locale, String messageKey) {
        return this.comI18nDictLangInfoDao.getI18nMessage(locale.getLanguage(), messageKey);
    }

    @Override
    public void afterSingletonsInstantiated() {
        this.comI18nDictLangInfoDao = this.applicationContext.getBean(ComI18nDictLangInfoDao.class);
    }
}
